package com.neuedu.oasystem.view;

import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Customer;
import com.neuedu.oasystem.service.CustomerService;
import com.neuedu.oasystem.service.impl.CustomerServiceImpl;

public class CustomerView {

	private Scanner scanner = null;
	private static CustomerService customerService;

	public void init() {
		System.out.println("========================================");
		System.out.println("            欢迎使用客户管理                                     ");
		System.out.println("========================================");
		System.out.println("1.查看客户信息                                                                           ");
		System.out.println("2.新增客户信息                                                                           ");
		System.out.println("3.修改客户信息                                                                           ");
		System.out.println("4.删除客户信息                                                                           ");
		System.out.println("5.退出公告栏界面                                                                       ");
		System.out.println("========================================");
	}

	public void work() {

		scanner = new Scanner(System.in);
		customerService = new CustomerServiceImpl();

		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("查看客户信息");
				findAllCustomer();
				break;

			case "2":
				System.out.println("新增客户信息");
				addCustomer();
				break;
			case "3":
				System.out.println("修改客户信息");
				editCustomer();
				break;
			case "4":
				System.out.println("删除客户信息");
				deleteCustomer();
				break;
			case "5":
				System.out.println("您选择了退出公告栏界面！");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！请重新输入：");
				break;
			}
		}
	}

	private void findAllCustomer() {
		List<Customer> list = customerService.findAll();
		for (Customer i : list) {
			System.out.println(i.getCustomerId() + "," + i.getCustomerName()
					+ "," + i.getCustomerPosition() + "," + i.getCustomerSex()
					+ "," + i.getCustomerUnit() + "," + i.getCustomerAddress()
					+ "," + i.getCustomerPhone() + "," + i.getCustomerEmail());
		}

	}

	private void deleteCustomer() {
		Boolean running = true;
		outer1: while (running) {
			System.out.println("========================================");
			System.out.println("            欢迎使用修改客户信息                                ");
			System.out.println("=============================================");

			System.out.println("请输入要删除客户的ID:          ");
			String customerIdString = scanner.next();
			do {
				try {
					if (Integer.valueOf(customerIdString) instanceof Integer) {
						break;
					} else {
						customerIdString = scanner.next();
					}
				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入：");
				}
			} while (true);
			int customerId = Integer.parseInt(customerIdString);
			while (true) {
				System.out.println("请选择：");
				System.out
						.println("1.确认删除 ！                                                                            ");
				System.out
						.println("2.返回到客户信息管理界面 ！                                                ");
				String choose = scanner.next();
				outer: switch (choose) {
				case "1":

					Boolean bool = customerService.deleteById(customerId);
					if (bool) {
						System.out.println("删除成功！");
					} else {
						System.out.println("删除失败！");
					}
					running = false;

					break outer1;
				case "2":
					if (running) {
						System.out.println("返回到客户信息管理界面,客户还未被删除,您确定返回吗？ ");
						while (running) {
							System.out.println("请选择：");
							System.out.println("1.确定");
							System.out.println("2.取消");
							String choose1 = scanner.next();
							switch (choose1) {
							case "1":
								System.out.println("1.您已经返回到客户信息管理界面！");

								break outer1;
							case "2":
								System.out.println("您已经取消!");

								break outer;
							default:
								System.out.println("您的输入有误！");
								running =false;
								break;
							}
						}
					}
					break;
				default:
					System.out.println("您的输入有误！");
					break;
				}
			}
		}

	}

	private void editCustomer() {
		Customer customer = null;
		Boolean running = true;
		outer1: while (running) {
			System.out.println("========================================");
			System.out.println("                                欢迎使用修改客户信息 ！                               ");
			System.out.println("========================================");

			System.out.println("请输入要修改客户的ID:          ");
			System.out.println("========================================");
			String customerIdString;
			do {
				try {
					customerIdString = scanner.next();
					if (Integer.valueOf(customerIdString)instanceof Integer) {
						break;
					} else {
						customerIdString = scanner.next();
					}
				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入：");
				}
			} while (true);
			int customerId = Integer.parseInt(customerIdString);
			System.out
					.println("请输入客户姓名 ：                             ");
			String customerNameString = scanner.next();
			System.out
					.println("请输入客户职务：                              ");
			String customerPosition = scanner.next();
			System.out.println("请输入新建客户的性别（男请输入1，女请输入0）：");
			int customerSexInt ;
			boolean aboolea = false;
			a:do {
				
				customerSexInt = scanner.nextInt();
				if (customerSexInt == 1 ||customerSexInt == 0) {
					aboolea = true;
				} else {
					System.out.println("输入错误，请重新输入：");
					break a;
				}
			} while (aboolea==false);
			String customerSex = Integer.toString(customerSexInt);
			System.out
					.println("请输入客户单位:                                 ");
			String customerUnit = scanner.next();
			System.out
					.println("请输入客户单位地址:                                 ");
			String customerAddress = scanner.next();
			System.out
					.println("请输入客户电话:                                 ");
			String customerPhoneString = scanner.next();
			System.out.println("请输入修改客户的邮箱：");
			String customerEmail;
			boolean bools = false; 
			do {
				customerEmail = scanner.next();
				if (customerEmail.indexOf("@")==-1||customerEmail.indexOf(".")==-1) {
					System.out.println("邮箱格式错误：没有@或.");
				} else if (customerEmail.indexOf("@")!= customerEmail.lastIndexOf("@")||customerEmail.indexOf(".")!= customerEmail.lastIndexOf(".")) {
					System.out.println("邮箱格式错误：含有多个@或.");
				} else if (customerEmail.indexOf("@")>customerEmail.indexOf(".")) {
					System.out.println("邮箱格式错误：.在@前面");
				} else if (!checkEmailContent(customerEmail)) {
					System.out.println("邮箱格式错误：包含特殊字符");
				} else {
					System.out.println("有效邮箱！");
					bools = true;
				}
			} while (bools==false);
			while (true) {
				System.out.println("请选择：");
				System.out
						.println("1.确认修改 ！                                                                            ");
				System.out
						.println("2.返回到客户信息管理界面 ！                                                ");
				String choose = scanner.next();
				outer: switch (choose) {
				case "1":
					customer = new Customer(customerId, customerNameString,
							customerPosition, customerSex, customerUnit,
							customerAddress, customerPhoneString, customerEmail);
					boolean bool = customerService.edit(customer);
					customerService.findById(customerId);
					if (bool) {
						System.out.println("修改成功！");
					} else {
						System.out.println("修改失败！");
					}
					running = false;

					break outer1;
				case "2":
					if (running) {
						System.out
								.println("返回到客户信息管理界面,会使得您刚新增的客户信息还没添加,您确定返回吗？ ");
						while (running) {
							System.out.println("请选择：");
							System.out.println("1.确定");
							System.out.println("2.取消");
							String choose1 = scanner.next();
							switch (choose1) {
							case "1":
								System.out.println("1.您已经返回到客户信息管理界面！");

								break outer1;
							case "2":
								System.out.println("您已经取消!");

								break outer;
							default:
								System.out.println("您的输入有误！");
								break;
							}
						}
					}
					break;
				default:
					System.out.println("您的输入有误！");
					running = false;
					break;
				}
			}
		}

	}

	private void addCustomer() {
		boolean runnning = true;
		Customer customer = null;
		outer1:while (runnning) {
			System.out.println("========================================");
			System.out.println("                                欢迎使用新建客户信息 ！                               ");
			System.out.println("========================================");
			System.out.println("请输入新建客户ID：                ");
			String customerIdString;
			do {
				try {
					customerIdString = scanner.next();
					if (Integer.valueOf(customerIdString)instanceof Integer) {
						break;
					} else {
						customerIdString = scanner.next();
					}
				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入：");
				}
			} while (true);
			int customerId = Integer.parseInt(customerIdString);
			System.out.println("请输入新建客户姓名：");
			String customerName = scanner.next();
			System.out.println("请输入新建客户职务：");
			String customerPosition = scanner.next();
			System.out.println("请输入新建客户的性别（男请输入1，女请输入0）：");
			int customerSexInt ;
			boolean aboolea = false;
			a:do {
				
				customerSexInt = scanner.nextInt();
				if (customerSexInt == 1 ||customerSexInt == 0) {
					aboolea = true;
				} else {
					System.out.println("输入错误，请重新输入：");
					break a;
				}
			} while (aboolea==false);
			String customerSex = Integer.toString(customerSexInt);
			System.out.println("请输入新建客户的单位：");
			String customerUnit = scanner.next();
			System.out.println("请输入新建客户单位地址：");
			String customerAddress = scanner.next();
			System.out.println("请输入新建客户的电话：");
			String customerPhone = scanner.next();
			System.out.println("请输入新建客户的邮箱：");
			String customerEmail;
			boolean bools = false; 
			do {
				customerEmail = scanner.next();
				if (customerEmail.indexOf("@")==-1||customerEmail.indexOf(".")==-1) {
					System.out.println("邮箱格式错误：没有@或.");
				} else if (customerEmail.indexOf("@")!= customerEmail.lastIndexOf("@")||customerEmail.indexOf(".")!= customerEmail.lastIndexOf(".")) {
					System.out.println("邮箱格式错误：含有多个@或.");
				} else if (customerEmail.indexOf("@")>customerEmail.indexOf(".")) {
					System.out.println("邮箱格式错误：.在@前面");
				} else if (!checkEmailContent(customerEmail)) {
					System.out.println("邮箱格式错误：包含特殊字符");
				} else {
					System.out.println("有效邮箱！");
					bools = true;
				}
			} while (bools==false);
			while (true) {
				
				System.out.println("1.新建并退出1");
				System.out.println("2.返回公告管理界面！");
				System.out.println("请选择：");
				String choose = scanner.next();
				outer:switch (choose) {
				case "1":
					customer = new Customer(customerId,customerName,customerPosition,customerSex,customerUnit,customerAddress,customerPhone,customerEmail);
					boolean bool = customerService.add(customer);
					if (bool) {
						System.out.println("新建成功!");
					} else {
						System.out.println("新建失败!");
					}
					runnning = false;
					break outer1;
                 case "2":
                	 if (runnning) {
						System.out.println("返回公告会使您的新建客户数据丢失，您确定返回么？");
					
						while (runnning) {
							System.out.println("请选择：");
							System.out.println("1.确定");
							System.out.println("2.取消");
							String choose1 = scanner.next();
							switch (choose1) {
							case "1":
								System.out.println("1.您已经返回客户信息管理界面!");
								break outer1;

							case "2":
								System.out.println("您已经取消！");
								break outer;
							default:
								System.out.println("您的输入有误！");
								break;
							}
						}
                	 }
				
                	 break;
				default:
					System.out.println("您的输入有误！");
					runnning =false;
					break;
				}
			}
		}
	}

	private static boolean checkEmailContent(String email) {
		for (int i = 0; i < email.length(); i++) {
			char c = email.charAt(i);
			if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '@'
					|| c == '.' || c >= '0' && c <= '9') {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

}
